package com.lu.system.user.controller;

import java.util.List;
import java.util.Map;

import com.lu.common.utils.PageUtils;
import com.lu.common.utils.Query;
import com.lu.common.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lu.system.user.domain.MenuDO;
import com.lu.system.user.service.MenuService;

/**
 * 菜单管理
 * 
 * @author zhanglu
 * @email 1309617271@qq.com
 * @date 2018-09-20 21:05:03
 */
 
@Controller
@RequestMapping("/system/menu")
public class MenuController {
	@Autowired
	private MenuService menuService;

   /**
    *  根据id查询
	*  @return
	*/
    @ResponseBody
    @GetMapping("/get")
    public R get(Long menuId){
        MenuDO menu = menuService.get(menuId);
        R r = new R();
        r.put("data",menu);
        return r;
    }

	/**
	 * 查询列表数据
	 * @param params
	 * @return
	 */
	@ResponseBody
	@GetMapping("/list")
	public PageUtils getMenuList(@RequestParam Map<String, Object> params){
        Query query = new Query(params);
		List<MenuDO> menuList = menuService.list(query);
		int total = menuService.count(query);
		PageUtils pageUtils = new PageUtils(menuList, total);
		return pageUtils;
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save( MenuDO menu){
		if(menuService.save(menu)>0){
			return R.ok();
		}
		return R.error();
	}

    /**
     * 批量保存
     */
    @ResponseBody
    @PostMapping("/saveBatch")
    public R saveBatch( List<MenuDO> menu){
        try {
			menuService.saveBatch(menu);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
        return R.ok();
    }

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( MenuDO menu){
		menuService.update(menu);
		return R.ok();
	}

    /**
     * 批量修改
     */
    @ResponseBody
    @PostMapping("/updateBatch")
    public R updateBatch( List<MenuDO> menu){
        try {
			menuService.updateBatch(menu);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
        return R.ok();
    }

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ResponseBody
	public R remove( Long menuId){
		if(menuService.remove(menuId)>0){
			return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 批量删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Long[] menuIds){
		menuService.batchRemove(menuIds);
		return R.ok();
	}
	
}
